package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetmanage.mapper.AdminMapper;
import org.csu.jpetmanage.mapper.SignonMapper;
import org.csu.jpetmanage.mapper.UserMapper;
import org.csu.jpetmanage.model.Admin;
import org.csu.jpetmanage.model.Order;
import org.csu.jpetmanage.model.Signon;
import org.csu.jpetmanage.model.User;
import org.csu.jpetmanage.service.AdminService;
import org.csu.jpetmanage.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.SQLOutput;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    public UserService userService;

    @Autowired
    UserMapper userMapper;
    @Autowired
    SignonMapper signonMapper;

    @Autowired
    User user;
    @Autowired
    Signon signon;

    @GetMapping("/UserManageIndex")
    public String userIndex(Model model){
        List<User> userList = userMapper.selectList(null);//查询所有的list记录
        model.addAttribute("userList",userList);
        return "UserManageIndex";
    }

    @RequestMapping("/userAdd")
    public String userAdd(
            @RequestParam("userid") String userid,
            @RequestParam("email") String email,
            @RequestParam("firstname") String firstname,
            @RequestParam("lastname") String lastname,
            @RequestParam("addr1") String addr1,
            @RequestParam("addr2") String add2,
            @RequestParam("city") String city,
            @RequestParam("state") String state,
            @RequestParam("zip") String zip,
            @RequestParam("country") String country,
            @RequestParam("phone") String phone,
            @RequestParam("password")String password,
            Model model, HttpSession session){
        user.setUserid(userid);
        user.setEmail(email);
        user.setFirstname(firstname);
        user.setLastname(lastname);
        user.setState(state);
        user.setAddr1(addr1);
        user.setAddr2(add2);
        user.setCity(city);
        user.setZip(zip);
        user.setCountry(country);
        user.setPhone(phone);
      userMapper.insert(user);

      //加密并放入signon

        String passwordAfter =userService.encrypt(password);
        signon.setPassword(passwordAfter);
        signon.setUsername(userid);
      signonMapper.insert(signon);

        session.setAttribute("msg","插入成功");
        return "redirect:/UserManageIndex";
    }

    @RequestMapping("/userDelete")
    public String userDelete(
            @RequestParam("userid")String userid,
            HttpSession session
    ){
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("userid",userid);
        int sum = userMapper.deleteByMap(hashMap);
        System.out.println("sum is :"+sum);
        if (sum>=1){
            session.setAttribute("msg","删除成功");
        }
        HashMap<String,Object>hashMap1=new HashMap<>();
        hashMap1.put("username",userid);
        int sum1=signonMapper.deleteByMap(hashMap1);
        System.out.println("sum1 is :"+sum1);

        return "redirect:/UserManageIndex";
    }

    @GetMapping("/userEditAjax")
    @ResponseBody
    public Map<String,Object> userEditAjax(
            @RequestParam("userid") String userid,
            HttpSession session
    ){  //user表
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userid);//是根据User实体类中的属性来进行取值
        List<User> userList = userMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (userList.size()==1){
            System.out.println("userList size is 1");
            user = userList.get(0);
        //signon表
        QueryWrapper<Signon>queryWrapper1= new QueryWrapper<>();
        queryWrapper1.eq("username",userid);
        List<Signon> signonList=signonMapper.selectList(queryWrapper1);
        Map<String,Object> map1= new HashMap<>();
            signon = signonList.get(0);

            session.setAttribute("editUser",user);
            session.setAttribute("editSignon",signon);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("userid",user.getUserid());
            map.put("lastname",user.getLastname());
            map.put("firstname",user.getFirstname());
            map.put("addr1",user.getAddr1());
            map.put("addr2",user.getAddr2());
            map.put("city",user.getCity());
            map.put("zip",user.getZip());
            map.put("country",user.getCountry());
            map.put("state",user.getState());
            map.put("phone",user.getPhone());

            return map;
        }
        return map;
    }

    @RequestMapping("userEdit")
    public String userEdit(
            @RequestParam("editusername") String username,
            @RequestParam("editfirstName") String firstName,
            @RequestParam("editlastName") String lastName,
            @RequestParam("editAddress1") String Address1,
            @RequestParam("editAddress2") String Address2,
            @RequestParam("editCountry") String Country,
            @RequestParam("editCity") String City,
            @RequestParam("editZip") String Zip,
            @RequestParam("editState") String State,
            @RequestParam("editPhone") String Phone,
            @RequestParam("editPassword")String password,

            Model model, HttpSession session
    ){
        user = (User)session.getAttribute("editUser");
        user.setUserid(username);
        user.setFirstname(firstName);
        user.setLastname(lastName);
        user.setAddr1(Address1);
        user.setAddr2(Address2);
        user.setCountry(Country);
        user.setCity(City);
        user.setZip(Zip);
        user.setState(State);
        user.setPhone(Phone);

        if(password.trim().equals("")||password!=null)
        {    signon =(Signon)session.getAttribute("editSignon") ;
            signon.setUsername(username);
            String passwordNew= userService.encrypt(password);
            signon.setPassword(passwordNew);
            UpdateWrapper<Signon> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("username",signon.getUsername());
            int rows = signonMapper.update(signon,updateWrapper);
            System.out.println("Edited signon rows number is: "+rows);

        }

        System.out.println(user.getUserid());
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userid",user.getUserid());
        int rows = userMapper.update(user,updateWrapper);
        System.out.println("Edited rows number is: "+rows);
        session.setAttribute("msg","修改成功");
        return "redirect:/UserManageIndex";
    }

    @RequestMapping("/searchUser")
    public String searchItem(
            @RequestParam("keyword") String keyword,
            Model model){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        System.out.println(keyword);
        queryWrapper.like("userid",keyword).
                or().like("firstname",keyword).
                or().like("addr1",keyword).
                or().like("phone",keyword).
                or().like("email",keyword);


        List<User> userList=userMapper.selectList(queryWrapper);
        model.addAttribute("userList" ,userList);
        return "UserManageIndex";
    }

    @GetMapping("/UserSearchAjax")
    @ResponseBody
    public List<User> orderSearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        System.out.println(keyword+" in OrderController");
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("userid",keyword).
                or().like("firstname",keyword).
                or().like("addr1",keyword).
                or().like("phone",keyword).
                or().like("email",keyword);
        return userMapper.selectList(queryWrapper);
    }
}
